/**
 * Created by Administrator on 2018/12/12.
 */
import Vue from 'vue';
import axios from 'axios';
import ElementUI from 'element-ui';
import MessageBox from 'element-ui';
Vue.use(ElementUI);
import router from '../router';
import {strToHexCharCode,toFixedBit,dec2hex,formatTime,getNewDate,formatDateTime } from  './index.js';
import MD5 from 'blueimp-md5';
var mdSecret ='fangyou968123456';
axios.interceptors.request.use(function (config) {
  var currentTime =parseInt(getNewDate((formatTime(new Date()))).getTime()/1000)
  config.headers={
    token:toFixedBit(strToHexCharCode(`${mdSecret}${currentTime}`),52,true,0,true),
    timeStamp:parseInt(getNewDate((formatTime(new Date()))).getTime()/1000),
    sign:MD5(`${localStorage.getItem('user_name')}${localStorage.getItem('token')}${currentTime}`)
  }
  // // 在发送请求之前做些什么
  return config;
}, function (error) {
  // 对请求错误做些什么
  return Promise.reject(error);
});


axios.interceptors.response.use(data => {// 响应成功关闭loading
  if(data.data.code=="250001"||data.data.code=="100005"){
    router.push({
      path:'/'
    })
  }else if(data.data.code=="230008"){
    router.push({
      path:'/401'
    })
  }else if(data.data.code=="250002"){
    alert('该账号已在其他电脑上登录，请重新登录');
    setTimeout(function(){
        router.push({
          path:'/'
        })
      },
      2000);
  }
  return data
}, error => {

  // Vue.$alert('加载失败,请稍后再试');
  return Promise.reject(error)
});

export  default axios;
